package com.kk.permission.huawei.actionutil;

import android.accessibilityservice.AccessibilityService;
import android.view.accessibility.AccessibilityNodeInfo;

import com.kk.R;
import com.kk.permission.accessibilityutils.NodeUtil;
import com.kk.permission.huawei.HuaweiActionUtil;
import com.kk.permission.huawei.HuaweiTool;

import java.util.HashMap;
import java.util.List;

public class HuaweiAutoStartUtil {
    private final String STEP1 = "step1";
    private final String STEP2 = "step2";
    private final String STEP3 = "step3";
    private final String STEP4 = "step4";
    private final String STEP5 = "step5";
    private final String STEP6 = "step6";
    private HashMap<String, String> mStepMap = new HashMap();
    private boolean result = false;

    public boolean auto(AccessibilityNodeInfo accessibilityNodeInfo, AccessibilityService accessibilityService) {
        if (this.result) {
            return true;
        }
        if (NodeUtil.pageContains(accessibilityNodeInfo, "手动管理") && NodeUtil.pageContains(accessibilityNodeInfo, "允许自启动") && NodeUtil.pageContains(accessibilityNodeInfo, "允许关联启动") && NodeUtil.pageContains(accessibilityNodeInfo, HuaweiTool.getString(R.string.ok))) {
            if (!this.mStepMap.containsKey("auto_step_switch_1")) {
                HuaweiActionUtil.performSwitch(accessibilityNodeInfo, "允许自启动");
                this.mStepMap.put("auto_step_switch_1", "1");
                return false;
            } else if (this.mStepMap.containsKey("auto_step_switch_1") && !this.mStepMap.containsKey("auto_step_switch_2")) {
                HuaweiActionUtil.performSwitch(accessibilityNodeInfo, "允许关联启动");
                this.mStepMap.put("auto_step_switch_2", "1");
                return false;
            } else if (this.mStepMap.containsKey("auto_step_switch_1") && this.mStepMap.containsKey("auto_step_switch_2") && !this.mStepMap.containsKey("auto_step_switch_3")) {
                HuaweiActionUtil.performSwitch(accessibilityNodeInfo, "允许后台活动");
                this.mStepMap.put("auto_step_switch_3", "1");
                return false;
            } else if (this.mStepMap.containsKey("auto_step_switch_1") && this.mStepMap.containsKey("auto_step_switch_2") && this.mStepMap.containsKey("auto_step_switch_3")) {
                HuaweiTool.actionAutoStartDone();
                NodeUtil.back(accessibilityService);
                return false;
            }
        }
        if (NodeUtil.pageContains(accessibilityNodeInfo, HuaweiTool.getString(R.string.accessiblity_permission_huawei_title_auto_start)) || NodeUtil.pageContains(accessibilityNodeInfo, HuaweiTool.getString(R.string.accessiblity_permission_huawei_app_autoboot)) || NodeUtil.pageContains(accessibilityNodeInfo, "应用启动管理") || NodeUtil.pageContains(accessibilityNodeInfo, "启动管理")) {
            if (this.mStepMap.containsKey("auto_step_switch_1") && this.mStepMap.containsKey("auto_step_switch_2") && this.mStepMap.containsKey("auto_step_switch_3")) {
                this.result = true;
                HuaweiTool.actionAutoStartDone();
                NodeUtil.back(accessibilityService);
            } else {
                List findAccessibilityNodeInfosByText = accessibilityNodeInfo.findAccessibilityNodeInfosByText(HuaweiTool.getAppName());
                if (findAccessibilityNodeInfosByText == null || findAccessibilityNodeInfosByText.size() <= 0) {
                    HuaweiActionUtil.scrollForward(accessibilityNodeInfo);
                } else {
                    accessibilityNodeInfo = HuaweiActionUtil.getSwitch((AccessibilityNodeInfo) findAccessibilityNodeInfosByText.get(0));
                    if (!(accessibilityNodeInfo == null || this.mStepMap.containsKey("auto_step_click_item"))) {
                        this.mStepMap.put("auto_step_click_item", "1");
                        NodeUtil.click(accessibilityNodeInfo, 300);
                    }
                }
            }
        }
        return false;
    }

    public boolean autoStart2(AccessibilityNodeInfo accessibilityNodeInfo, AccessibilityService accessibilityService) {
        if (this.result) {
            return true;
        }
        if (NodeUtil.pageContains(accessibilityNodeInfo, HuaweiTool.getString(R.string.accessiblity_permission_huawei_auto_start_pagetitle))) {
            if (!this.mStepMap.containsKey("step1")) {
                List findAccessibilityNodeInfosByText = accessibilityNodeInfo.findAccessibilityNodeInfosByText(HuaweiTool.getAppName());
                if (findAccessibilityNodeInfosByText == null || findAccessibilityNodeInfosByText.size() <= 0) {
                    if (!this.mStepMap.containsKey("step1")) {
                        HuaweiActionUtil.scrollForward(accessibilityNodeInfo);
                    }
                } else if (!this.mStepMap.containsKey("step1")) {
                    NodeUtil.clickByText(accessibilityNodeInfo, HuaweiTool.getAppName());
                    this.mStepMap.put("step1", "1");
                }
            } else if (this.mStepMap.containsKey("step2") && !this.mStepMap.containsKey("step3")) {
                NodeUtil.back(accessibilityService);
                this.result = true;
                this.mStepMap.put("step3", "1");
                HuaweiTool.actionAutoStartDone();
            }
        } else if (NodeUtil.pageContains(accessibilityNodeInfo, HuaweiTool.getString(R.string.huawei_mha_al00_confirm)) && NodeUtil.pageContains(accessibilityNodeInfo, HuaweiTool.getAppName()) && NodeUtil.pageContains(accessibilityNodeInfo, "4") && this.mStepMap.containsKey("step1") && !this.mStepMap.containsKey("step2")) {
            HuaweiActionUtil.clickByTextAll(accessibilityNodeInfo, HuaweiTool.getString(R.string.huawei_mha_al00_confirm));
            this.mStepMap.put("step2", "1");
        }
        return false;
    }

    public boolean autoStart3(AccessibilityNodeInfo accessibilityNodeInfo, AccessibilityService accessibilityService) {
        if (this.result) {
            return true;
        }
        if (NodeUtil.pageContains(accessibilityNodeInfo, "自动启动管理")) {
            if (!this.mStepMap.containsKey("step1")) {
                List findAccessibilityNodeInfosByText = accessibilityNodeInfo.findAccessibilityNodeInfosByText(HuaweiTool.getAppName());
                if (findAccessibilityNodeInfosByText == null || findAccessibilityNodeInfosByText.size() <= 0) {
                    if (!this.mStepMap.containsKey("step1")) {
                        HuaweiActionUtil.scrollForward(accessibilityNodeInfo);
                    }
                } else if (!this.mStepMap.containsKey("step1")) {
                    NodeUtil.clickByText(accessibilityNodeInfo, HuaweiTool.getAppName());
                    this.mStepMap.put("step1", "1");
                }
            } else if (this.mStepMap.containsKey("step2") && !this.mStepMap.containsKey("step3")) {
                NodeUtil.back(accessibilityService);
                this.result = true;
                this.mStepMap.put("step3", "1");
                HuaweiTool.actionAutoStartDone();
            }
        } else if (NodeUtil.pageContains(accessibilityNodeInfo, HuaweiTool.getString(R.string.huawei_mha_al00_confirm)) && NodeUtil.pageContains(accessibilityNodeInfo, HuaweiTool.getString(R.string.accessiblity_permission_huawei_doze_all_ok)) && NodeUtil.pageContains(accessibilityNodeInfo, HuaweiTool.getAppName()) && NodeUtil.pageContains(accessibilityNodeInfo, "4")) {
            if (this.mStepMap.containsKey("step1") && !this.mStepMap.containsKey("step2")) {
                HuaweiActionUtil.clickByTextAll(accessibilityNodeInfo, HuaweiTool.getString(R.string.accessiblity_permission_huawei_doze_all_ok));
                this.mStepMap.put("step2", "1");
            }
        } else if (NodeUtil.pageContains(accessibilityNodeInfo, HuaweiTool.getString(R.string.huawei_mha_al00_confirm)) && NodeUtil.pageContains(accessibilityNodeInfo, "取消") && NodeUtil.pageContains(accessibilityNodeInfo, HuaweiTool.getAppName()) && NodeUtil.pageContains(accessibilityNodeInfo, "4") && this.mStepMap.containsKey("step1") && !this.mStepMap.containsKey("step2")) {
            HuaweiActionUtil.clickByTextAll(accessibilityNodeInfo, HuaweiTool.getString(R.string.huawei_mha_al00_confirm));
            this.mStepMap.put("step2", "1");
        }
        return false;
    }
}
